<template>
  <div id="login">
    <div class="rootMain">
      <div class="login">
        <div class="leftTxt" @click="$router.go(-1)">
          <!-- <div class="leftTxt" v-if="!qrcode" @click="$router.go(-1)"> -->
          <van-icon name="arrow-left" />
        </div>
        <div class="rightTxt" @click="show = true;">{{$t('m.language')}}</div>
        <div class="topImg">
          <div>
            <img src="@/assets/img/fuze/icon200.png" alt />
          </div>
        </div>
        <div class="con">
          <div class="form">
            <!-- <div style="width:0;height:0;overflow:hidden;opacity:0;margin:0;padding:0;">
              <input type="text" />
              <input type="password" />
            </div>-->
            <p>{{$t('m.phoneT')}}</p>
            <div>
              <span class="selectPhone">+86</span>
              <input
                type="text"
                @keyup.enter="reg"
                v-model="phone"
                @blur="verifyUser"
                :placeholder="$t('m.phoneP')"
              />
            </div>
            <div>
              <span class="iconfont icon-mima"></span>
              <input
                @keyup.enter="reg"
                type="text"
                v-if="showPwd"
                v-model="pwd"
                :placeholder="$t('m.pwdP')"
              />
              <input
                @keyup.enter="reg"
                type="password"
                v-else
                v-model="pwd"
                :placeholder="$t('m.pwdP')"
              />
              <label :class="[showPwd?'show':'']" @click="showPwd=!showPwd"></label>
            </div>
            <p class="tips">{{$t('m.pwdTips')}}</p>
            <div>
              <span class="iconfont icon-anquan1"></span>
              <input type="number" v-model="code" :placeholder="$t('m.codeP')" @keyup.enter="reg" />
              <div @click="hasRegister?hasToast():getCode()">{{val}}</div>
            </div>
            <div>
              <span class="iconfont icon-yaoqingma"></span>
              <input
                type="text"
                v-model="qrcode"
                :disabled="isDisabled"
                :placeholder="$t('m.qrcodeP')"
                @keyup.enter="reg"
              />
              <!-- <span
                class="defaultCode iconfont icon-down-fill"
                v-show="selectCode"
                @click="showCode"
              ></span>-->
            </div>
          </div>
          <div class="ruler">
            <b :class="[read?'checked':'']" @click="read=!read"></b>
            <p>
              {{$t('m.rulerP')}}
              <label @click="showuserAgreement=true;">{{$t('m.agreement')}}</label>
            </p>
          </div>
          <div class="btnBox">
            <div @click="hasRegister?hasToast():reg()">{{$t('m.signup')}}</div>
            <!-- <p @click="regEmail">{{$t('m.emailReg')}}</p> -->
          </div>
        </div>
      </div>
      <van-popup class="langPop" v-model="show">
        <div>
          <p @click="changeLang(0)">中文简体</p>
          <p @click="changeLang(1)">English</p>
        </div>
      </van-popup>
      <van-popup class="codePop" v-model="showDefaultCode" position="bottom">
        <div>
          <div class="codeTitle">VC{{$t('m.accountP3')}}{{$t('m.invitationCode1')}}</div>
          <div class="codeList">
            <p @click="select">BzkRCV</p>
          </div>
        </div>
      </van-popup>
    </div>
    <userAgreement v-if="showuserAgreement" @closeuserAgreement="closeuserAgreement"></userAgreement>
    <RootLoading v-show="rootLoading"></RootLoading>
  </div>
</template>
<script>
import Services from "@/service/index.js";
import Header from "@/components/header";
import Footer from "@/components/footer";
import userAgreement from "@/components/userAgreement";
import RootLoading from "@/components/rootLoading";
import { Dialog, Toast } from "vant";
export default {
  data() {
    return {
      phone: "",
      pwd: "",
      code: "",
      qrcode: "", //邀请码
      showPwd: false,
      isDisabled: false,
      read: true,
      showuserAgreement: false,
      time: 60,
      val: "",
      flag: true,
      hasRegister: false,
      rootLoading: false,
      canReg: false,
      show: false,
      showDefaultCode: false,
      selectCode: true
    };
  },
  created() {
    this.val = this.$t("m.getQrcode");
    if (!!this.$route.query.code) {
      this.qrcode = this.$route.query.code;
      // this.isDisabled = true;
      // this.selectCode = false;
    } else {
      // this.qrcode = "BzkRCV";
    }
  },
  components: { userAgreement, RootLoading },
  computed: {},
  methods: {
    // 选择默认邀请码
    select() {
      this.qrcode = "BzkRCV";
      this.showDefaultCode = false;
    },
    // 显示选择邀请码弹窗
    showCode() {
      this.showDefaultCode = true;
    },
    // 切换语言
    changeLang(index) {
      if (index == 1) {
        this.$store.state.lang = "en_US";
        this.$i18n.locale = this.$store.state.lang; //关键语句
        localStorage.setItem("language", "en_US");
      } else {
        this.$store.state.lang = "zh_CN";
        this.$i18n.locale = this.$store.state.lang; //关键语句
        localStorage.setItem("language", "zh_CN");
      }
      this.show = false;
      this.$router.go(0);
    },
    closeuserAgreement(msg) {
      this.showuserAgreement = false;
    },
    verifyUser() {
      if (this.phone.length < 11) {
        return;
      }
      Services.getRegisterUser({
        mobile: this.phone,
        lang: localStorage.getItem("language")
      }).then(res => {
        if (res.code == "0000") {
          if (res.register) {
            if (!this.$route.query.code) {
              Toast(this.$t("m.hasRegisterP"));
            } else {
              Dialog.alert({
                message: this.$t("m.regAlert"),
                confirmButtonText: this.$t("m.download")
              }).then(() => {
                this.$router.push(`/download`);
              });
            }
            this.hasRegister = true;
            return;
          } else {
            this.hasRegister = false;
          }
        } else {
          Toast(res.msg);
        }
      });
    },
    hasToast() {
      Toast(this.$t("m.hasRegisterP"));
      return;
    },
    getCode() {
      if (!this.phone) {
        Toast(this.$t("m.phoneP"));
        return false;
      }
      if (this.hasRegister) {
        return false;
      } else {
        this.verifyUser();
      }
      if (this.flag) {
        this.flag = false;
        Services.registerVCode({
          mobile: this.phone,
          lang: localStorage.getItem("language")
        }).then(res => {
          if (res.code == "0000") {
            this.val = this.time + "s";
            const timer = setInterval(() => {
              if (this.time > 1) {
                this.time--;
                this.val = this.time + "s";
              } else {
                this.time = 60;
                this.val = this.$t("m.getQrcode");
                clearInterval(timer);
                this.flag = true;
              }
            }, 1000);
          } else {
            this.flag = true;
            Toast(res.msg);
          }
        });
      }
    },
    reg() {
      const regex = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
      if (!this.phone) {
        Toast(this.$t("m.phoneP"));
        return false;
      } else if (this.phone.length != 11) {
        Toast(this.$t("m.phoneP1"));
        return false;
      } else if (!this.pwd) {
        Toast(this.$t("m.pwdP"));
        return false;
      } else if (!regex.test(this.pwd)) {
        Toast(this.$t("m.pwdP1"));
        return false;
      } else if (!this.code) {
        Toast(this.$t("m.codeP"));
        return false;
      } else if (!this.qrcode) {
        Toast(this.$t("m.qrcodeP"));
        return false;
      } else if (!this.read) {
        Toast(this.$t("m.rulerP1"));
        return false;
      }
      this.canReg = true;
      this.rootLoading = true;
      Services.register({
        mobile: this.phone,
        password: this.pwd,
        smsCode: this.code,
        referrerCode: this.qrcode,
        lang: localStorage.getItem("language")
      }).then(res => {
        console.log("————————————————————————————————————————");
        console.log(res);
        if (res.code == "0000") {
          localStorage.setItem("token", res.token);
          localStorage.setItem("userObj", JSON.stringify(res.user));
          this.rootLoading = false;
          // Dialog.alert({
          //   message: this.$t("m.regAlert1"),
          //   confirmButtonText: this.$t("m.confirmButtonText")
          // }).then(() => {
          // if (this.$route.query.music == 1) {
          //   this.$router.push(`/music`);
          //   return;
          // }
          if (!!this.$route.query.code) {
            this.$router.push(`/download`);
          } else {
            this.$router.push(`/home`);
          }
          // });
        } else {
          this.rootLoading = false;
          Toast(res.msg);
        }
      });
    },
    // 邮箱注册
    regEmail() {
      if (this.$route.query.code) {
        this.$router.replace(`/emailReg?code=${this.$route.query.code}`);
      } else {
        this.$router.replace(`/emailReg`);
      }
    }
  }
};
</script>
<style lang="scss" scoped type="text/scss">
#login {
  height: 100%;
  .rootMain {
    overflow-y: auto;
    height: 100%;
  }
  .login {
    height: 100%;
    position: relative;
    .topImg {
      > div {
        border-radius: 50%;
        overflow: hidden;
        margin-bottom: 0.8rem;
      }
    }
    .leftTxt {
      position: absolute;
      left: 0.3rem;
      top: 0.3rem;
      color: #40404c;
      i {
        font-size: 0.32rem;
      }
    }
    .rightTxt {
      position: absolute;
      right: 0.3rem;
      top: 0.3rem;
      color: #40404c;
    }

    h1 {
      margin-top: 0.2rem;
      font-size: 0.4rem;
      font-weight: bold;
      letter-spacing: 0.05rem;
      text-align: center;
      color: #fff;
    }
    .con {
      background: #fff;
      min-height: calc(100% - 4.4rem);
    }
    .form {
      padding: 0.35rem 0.5rem;
      p {
        color: #40404c;
        font-size: 0.3rem;
        padding: 0.2rem 0;
      }
      > div {
        background: linear-gradient(to right, #f8f8f8, #d3d3d5);
        border-radius: 0.2rem;
        height: 0.75rem;
        display: flex;
        align-items: center;
        padding: 0 1rem;
        position: relative;
        margin-bottom: 0.3rem;
        overflow: hidden;
        span {
          position: absolute;
          top: 0;
          left: 0;
          width: 1rem;
          height: 100%;
          text-align: center;
          line-height: 0.75rem;
          font-size: 0.3rem;
          color: #e4b577;
          &.defaultCode {
            left: unset;
            right: 0;
          }
        }
        &:last-child {
          margin-bottom: 0;
        }
        &:nth-of-type(3) {
          padding-right: 1.75rem;
          span {
            font-size: 0.36rem;
          }
        }

        input {
          flex: 1;
          border: none;
          outline: none;
          height: 100%;
          background: transparent;
          padding: 0 0.2rem;
          color: #40404c;
          font-size: 0.26rem;
          &::-webkit-input-placeholder {
            color: #999;
          }
        }
        label {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          right: 0.3rem;
          width: 0.4rem;
          height: 0.4rem;
          background-repeat: no-repeat;
          background-position: center;
          background-size: 100%;
          background-image: url("~@/assets/img/common/buyan@2x.png");
          &.show {
            background-image: url("~@/assets/img/common/zhengyan@2x.png");
          }
        }
        div {
          position: absolute;
          right: 0;
          top: 0;
          width: 1.75rem;
          height: 100%;
          line-height: 0.75rem;
          text-align: center;
          color: #e4b577;
          border-left: 1px solid #cecece;
          background: transparent;
          &.disabled {
            color: #ddd;
          }
        }
      }
      .tips {
        color: #c0c1c4;
        padding: 0 0.3rem 0.2rem;
        font-size: 0.24rem;
      }
    }
    .btnBox {
      padding: 0.5rem;
      div {
        border-radius: 0.2rem;
        height: 0.75rem;
        line-height: 0.75rem;
        text-align: center;
        background: linear-gradient(to right, #43434e, #727181);
        color: #fff;
        font-size: 0.32rem;
        &.disabled {
          opacity: 0.4;
        }
      }
      p {
        color: #fff;
        text-align: center;
      }
    }
    .ruler {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 0.2rem;
      b {
        position: relative;
        width: 14px;
        height: 14px;
        background: #727181;
        transform: rotate(45deg);
        border: 1px solid #727181;
        border-radius: 2px;
        &.checked::before {
          content: "";
          display: block;
          width: 8px;
          height: 8px;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translateX(-50%) translateY(-50%);
          background: #e4b577;
          border-radius: 2px;
        }
      }
      p {
        max-width: 80%;
        margin-left: 0.15rem;
        color: #c0c1c4;
        label {
          color: #e4b577;
        }
      }
    }
  }
  .langPop {
    border-radius: 0.1rem;
    width: 70%;
    > div {
      padding: 0.2rem;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      p {
        width: 100%;
        text-align: center;
        height: 1rem;
        line-height: 1rem;
        border-bottom: 1px solid #ddd;
        &:last-child {
          border-bottom: none;
        }
      }
    }
  }
  .codePop {
    border-top-left-radius: 0.1rem;
    border-top-right-radius: 0.1rem;
    .codeTitle {
      height: 0.95rem;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: bold;
      font-size: 0.3rem;
      border-bottom: 0.03rem solid #ddd;
    }
    .codeList {
      padding: 0.5rem 0.3rem;
      p {
        height: 0.75rem;
        display: flex;
        justify-content: center;
        align-items: center;
        background: #f1f1f1;
      }
    }
  }
}
</style>